package com.example.nbb.Service;

import com.example.nbb.Dao.ShopDao;
import com.example.nbb.Entity.ResultEntity;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class ShopService {

    private static final Logger logger = LoggerFactory.getLogger(ShopService.class);//日志   getLogger需要传入当前的类名

    @Resource
    ShopDao shopDao;

    /**
     * 根据用户id查询购物车
     *
     * @param userId 用户id
     * @return 返回
     */
    public String getAll(int userId) {

        try {
            return ResultEntity.success(shopDao.selectAllByUserId(userId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据购物车id删除购物车
     *
     * @param shopId 购物车id
     * @return 返回
     */
    public String deleteShop(int shopId) {
        try {
            return ResultEntity.success(shopDao.deleteByShopId(shopId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 增加购物车
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return 返回
     */
    public String insectShopCard(int userId, int productId) {
        try {
            return ResultEntity.success(shopDao.insectShopCard(userId, productId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

}
